var win = Titanium.UI.currentWindow;      
//var data = [];
var noviceUM = [];

var pageHeading = Ti.UI.createLabel({
	text: 'novice UM',
	color:'#3E3F87',
	font:{
		fontSize:20,
		fontWeight:'bold'
	},
	top:5,
	textAlign: 'center',
	height:'auto'
});

win.add(pageHeading);
		
var makeRow = function(_params) {
			var row = Ti.UI.createTableViewRow({height: 'auto', layout: 'vertical'});
			
			var naslovLabel = Ti.UI.createLabel({
				text: _params.naslovLabel,
				font: {fontSize:15, fontWeight:'bold'},
				color: 'black',
				top: 5,
				left: 10,
				height:'auto'
			});
			row.add(naslovLabel);
			
			var datumLabel = Ti.UI.createLabel({
				text: _params.datumLabel,
				font: {fontSize:12,	fontWeight:'bold'},
				color: 'black',
				top: 5,
				left: 10,
				height:'auto'
			});
			row.add(datumLabel);
			
			var opisLabel = Ti.UI.createLabel({
				text: _params.opisLabel,
				font: {fontSize:11},
				color: 'black',
				top: 5,
				left: 10,
				height: 'auto'
			});
			row.add(opisLabel);
			
			var vsebinaLabel = Ti.UI.createLabel({
				text: _params.vsebinaLabel,
				font: {fontSize:9},
				color: 'black',
				top: 5,
				bottom: 5,
				left: 10,
				height: 'auto'
			});
			row.add(vsebinaLabel);
			
			return row;
		}; 

var loader = Ti.Network.createHTTPClient({
    
    	onload: function(e) {    	
	        var doc = this.responseXML.documentElement;
			var novice = doc.getElementsByTagName("novica");
			//var x = 0;			
			
			for (var i = 0; i < novice.length; i++)
			{
				var novica = novice.item(i);
				
				var naslov = novica.getElementsByTagName("naslov").item(0).text;
				var datum = novica.getElementsByTagName("datum").item(0).text;
				var opis = novica.getElementsByTagName("opis").item(0).text;
				var vsebina = novica.getElementsByTagName("vsebina").item(0).text;
				
				noviceUM.push(makeRow({
					rownum: i,
					naslovLabel: naslov,
					datumLabel: datum,
					opisLabel: opis,
					vsebinaLabel: vsebina,
				}));
					
				//var row = Ti.UI.createTableViewRow({height:'auto', layout:'vertical'});
				/*var label = {
					title:naslov, 
					index:i
				};
				
				label.fontSize = 18;
				label.fontWeight = 'bold';
				label.color = 'black';
				label.height = 'auto';
					
				//row.add(label);					
				data.push(label);*/				
			}
			
			var tableview = Titanium.UI.createTableView({
				data:noviceUM, 
				borderRadius:3,
				top: 35			
			});
			
			win.add(tableview);
			
			/*tableview.addEventListener('click', function(e)
			{
				var w = Ti.UI.createWindow({title:'Podrobno'});
				var twr = Titanium.UI.createTableViewRow({data:noviceUM[3]});
				var tw = Titanium.UI.createTableView({data:twr, borderRadius:3});
				var wb = Ti.UI.createWebView({url:e.row.url});
				w.add(tw);
				
				var b = Titanium.UI.createButton({
					title:'Close',
					style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
				});
				w.setLeftNavButton(b);
				b.addEventListener('click',function()
				{
					w.close();
				});
				w.open({modal:true});
				w.show();
			});*/
	    },
	    
	    onerror: function(e) {
	        // this function is called when an error occurs, including a timeout
	        Ti.API.debug(e.error);
	        alert('error');
	    },
	    
		onreadystatechange: function(e) {
			Ti.API.debug(loader.readyState);
		},
		
	    timeout:5000  /* in milliseconds */
});

loader.open("GET","https://164.8.251.99:9473/services/cir2012/SiNf_UMNews/SiNf_UMNews");
loader.send();